package com.zhijiaoyun.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.CommonUtils;
import com.zhijiaoyun.common.ResponseUtils;
import com.zhijiaoyun.common.ValidationUtils;
import com.zhijiaoyun.dao.IUserDao;
import com.zhijiaoyun.model.User;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.IUserService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "userService")
public class UserServiceImpl implements IUserService {

	@Resource
	IUserDao userDao;

	@Override
	public User selectUserById(long id) {
		return userDao.selectByPrimaryKey(id);
	}

	@Override
	public PageList<User> pageByParams(int pageIndex, int pageSize, String username) {
		int count = userDao.countByParams(username);
		PageData pageData = new PageData();
		List<User> list = new ArrayList<>();
		if (count > 0) {
			pageData = PageData.createPage(count, pageIndex, pageSize);
			list = userDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), username);
		}
		PageList<User> pageList = new PageList<>(list, pageData);
		return pageList;
	}

	@Override
	public void addUser(User user) {
		userDao.insert(user);
	}

	@Override
	public void updataUser(User user) {
		userDao.updateByPrimaryKeySelective(user);
	}

	@Override
	public void deleteUserById(long id) {
		User user = selectUserById(id);
		if (user != null) {
			delete(user);
		}
	}

	@Override
	public void delete(User user) {
		userDao.deleteByPrimaryKey(user.getUserId());
	}

	@Override
	public Integer countByIdName(long id, String name) {
		return userDao.countByIdName(id, name);
	}

	@Override
	public User getUserByUsername(String username) {
		return userDao.selectUserByUsername(username);
	}

	@Override
	public User getUserByUserId(Long userId) {
		return userDao.selectByPrimaryKey(userId);
	}

	@Override
	public User getUserByUsernameNotUserId(Long userId, String username) {
		return userDao.getUserByUsernameNotUserId(userId, username);
	}

	@Override
	public int getUserByKV(String typeName, String typeValue) {
		return userDao.selectByKV(typeName, typeValue);
	}

	@Override
	public User getUserByMobile(String mobile) {
		return userDao.selectByMobile(mobile);
	}

	@Override
	public User getUserByEmail(String email) {
		return userDao.selectByEmail(email);
	}

	@Override
	public int countAll() {
		return userDao.countAll();
	}

	@Override
	public Map<String, Object> pageByUserName(int pageIndex, int pageSize, String username) {
		PageList<User> userPage = pageByParams(pageIndex, pageSize, username);
		Map<String, Object> map = new HashMap<>();
		map.put("page", userPage.getPageData());
		map.put("list", userPage.getList());
		return ResponseUtils.responseSuccessMap(map);
	}

	@Override
	public Map<String, Object> login(String email, String password, String device, HttpServletResponse response) {
		try {
			email = URLDecoder.decode(email, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return ResponseUtils.responseLoginError();
		}

		User user = getUserByEmail(email);
		if (user == null) {
			return ResponseUtils.responseLoginError();
		}

		if (!user.getPassword().equals(CommonUtils.MD5(password))) {
			return ResponseUtils.responseLoginError();
		}

		Map<String, Object> map = new HashMap<>();
		String token = ValidationUtils.createJWT(user, ValidationUtils.DAY_ONE);
		map.put("token", token);
		user.setPassword("");
		map.put("user", user);

		Cookie cookie = new Cookie("token", token);
		cookie.setMaxAge(60 * 60 * 24 * 2);
		response.addCookie(cookie);
		return ResponseUtils.responseSuccessMap(map);
	}

	@Override
	public List<User> listAll() {
		return userDao.listAll();
	}

}
